/*
 * @(#) MenuServiceImpl.java 2018年1月25日 上午10:53:51
 *
 * Copyright 2018 CIMIP, Inc. All rights reserved.
 * H-VISIONS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package cn.hc.share.service.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.hc.share.dao.TagDao;
import cn.hc.share.dao.UserDao;
import cn.hc.share.entity.Tag;
import cn.hc.share.entity.User;
import cn.hc.share.service.TagSaveException;
import cn.hc.share.service.TagService;
import cn.hc.share.service.UserNotFoundException;
import cn.hc.share.service.UserService;
import cn.hc.share.util.StringUtil;

@Service("tagService1")
public class TagServiceImpl_bak implements TagService
{

	@Resource
	private TagDao tagDao;

	@Resource
	private UserDao userDao;

	// 新增一个标签
	public Tag saveTag(Tag tag)
	{
		if (tag == null)
		{
			throw new RuntimeException("标签信息不存在!");
		}
		int num = tagDao.addTag(tag);
		if (num != 1)
		{
			throw new TagSaveException("标签保存失败!");
		}
		return tag;
	}

	// 新增标签
	public String addTags(String tagNames, String builder)
	{
		String tagIdsForArticle = "";
		if (StringUtil.isNotNull(tagNames))
		{
			String[] tags = tagNames.split(",");
			for (String tag : tags)
			{
				try
				{
					tag = tag.trim();
					Tag tagForSearch = new Tag();
					tagForSearch.setName(tag);
					tagForSearch.setBuilder(builder);

					// 如果存在,不再新增
					List<Tag> findTag = tagDao.findTag(tagForSearch);
					
					if (findTag.size() > 0)
					{
						tagIdsForArticle += findTag.get(0).getId() + ",";
						continue;
					}
					String id = UUID.randomUUID().toString();
					Timestamp createTime = new Timestamp(System.currentTimeMillis());
					
					Tag tagAdd = new Tag();
					tagAdd.setId(id);
					tagAdd.setBuilder(builder);
					tagAdd.setCreateTime(createTime);
					tagAdd.setName(tag);

					int addTag = tagDao.addTag(tagAdd);
					if (addTag != 1)
					{
						System.out.println("保存标签:" + tag + "失败!");
					}
					tagIdsForArticle += id + ",";
					
				} catch (Exception e)
				{
					continue;
				}
			}
			System.out.println("tagIdsForArticle:"+tagIdsForArticle);
			tagIdsForArticle = tagIdsForArticle.substring(0, tagIdsForArticle.length()-1);
			System.out.println("tagIdsForArticle:"+tagIdsForArticle);
		}
		return tagIdsForArticle;
	}

	// 获取用户创建的标签
	public List<Tag> getTagsByUserId(String userId)
	{
		List<Tag> tags = null;
		if (StringUtil.isNotNull(userId))
		{
			User user = userDao.findUserById(userId);
			if (user == null)
			{
				throw new UserNotFoundException("用户不存在!");
			}
			Tag tag = new Tag();
			tag.setBuilder(userId);
			tags = tagDao.findTag(tag);
		}
		return tags;
	}

	@Override
	public String addNewTag(String tagNames, String builder)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String addTags(String tagNames, String builder, String articleId)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateTag(String tagId, String tagNewName, String type)
	{
		// TODO Auto-generated method stub
		return 0;
	}

}
